﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using NGE;
using MH;

namespace MH.PE
{
    public partial class Form1 : Form
    {


        public Form1()
        {
            InitializeComponent();
        }

        Props props;

        private void Form1_Load(object sender, EventArgs e)
        {
            props = new Props();
        }

        private void 打开OToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenFileDialog fd = new OpenFileDialog();
            fd.RestoreDirectory = true;
            fd.Filter = "*.nprp|*.nprp";
            if (fd.ShowDialog() == DialogResult.OK)
            {
                props = (Props)Util.LoadObject(fd.FileName);
                refresh_list();
            }
        }

        private void 保存SToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SaveFileDialog fd = new SaveFileDialog();
            fd.RestoreDirectory = true;
            fd.Filter = "*.nprp|*.nprp";
            if (fd.ShowDialog() == DialogResult.OK)
            {
                Util.SaveObject(fd.FileName, props);
                MessageBox.Show("保存成功");
            }

        }

        private void 新建NToolStripMenuItem_Click(object sender, EventArgs e)
        {
            props.Clear();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Prop prop = new Prop();
            try
            {

                prop.Name = textBox2.Text;
                try
                {
                    prop.Type = int.Parse(comboBox1.Text.Substring(0, 2));
                }
                catch
                {
                    prop.Type = int.Parse(comboBox1.Text);
                }
                prop.RequestLevel = int.Parse(textBox4.Text);
                prop.Weight = double.Parse(textBox5.Text);
                prop.Worth = int.Parse(textBox6.Text);

                prop.Script = richTextBox1.Text;
                prop.Description = richTextBox2.Text;

                if (textBox17.Text != string.Empty)
                {
                    prop.ICON = new NImage(textBox17.Text);
                }
                prop.Damage = double.Parse(textBox11.Text);
                prop.Defense = double.Parse(textBox10.Text);
                prop.Accurate = double.Parse(textBox9.Text);
                prop.Missrate = double.Parse(textBox8.Text);
                prop.RangeMin = int.Parse(textBox7.Text);
                prop.RangeMax = int.Parse(textBox3.Text); 
                prop.Time = int.Parse(textBox15.Text);

                prop.Load = double.Parse(textBox12.Text);
                prop.Endure = int.Parse(textBox16.Text);

                textBox23.Text = prop.Square + "";
                textBox22.Text = prop.SquareType + "";

            }
            catch { }

            props.Add(prop);
            refresh_list();

        }

        private void button4_Click(object sender, EventArgs e)
        {
            props.Del(listBox1.SelectedIndices[0]);
            refresh_list();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            Prop prop = new Prop();
            try
            {

                prop.Name = textBox2.Text;
                try
                {
                    prop.Type = int.Parse(comboBox1.Text.Substring(0, 2));
                }
                catch
                {
                    prop.Type = int.Parse(comboBox1.Text);
                }
                prop.RequestLevel = int.Parse(textBox4.Text);
                prop.Weight = double.Parse(textBox5.Text);
                prop.Worth = int.Parse(textBox6.Text);

                prop.Script = richTextBox1.Text;
                prop.Description = richTextBox2.Text;

                if (textBox17.Text != string.Empty)
                {
                    prop.ICON = new NImage(textBox17.Text);
                }

                prop.Damage = double.Parse(textBox11.Text);
                prop.Defense = double.Parse(textBox10.Text);
                prop.Accurate = double.Parse(textBox9.Text);
                prop.Missrate = double.Parse(textBox8.Text);
                prop.RangeMin = int.Parse(textBox7.Text);
                prop.RangeMax = int.Parse(textBox3.Text);
                prop.Time = int.Parse(textBox15.Text);

                prop.Load = double.Parse(textBox12.Text);
                prop.Endure = int.Parse(textBox16.Text);

                prop.SkinFileName = new FileIndexer(textBox18.Text).FullFileName;
                prop.BulletFileName = new FileIndexer(textBox14.Text).FullFileName;
                prop.HitFileName = new FileIndexer(textBox13.Text).FullFileName;

                prop.SkinSoundFileName = new FileIndexer(textBox21.Text).FullFileName;
                prop.BulletSoundFileName = new FileIndexer(textBox20.Text).FullFileName;
                prop.HitSoundFileName = new FileIndexer(textBox19.Text).FullFileName;

                prop.Square = int.Parse(textBox23.Text);
                prop.SquareType = int.Parse(textBox22.Text);

            }
            catch { }

            props.Items[listBox1.SelectedIndex] = prop;
        }

        void refresh_list()
        {
            listBox1.Items.Clear();

            if (props.Count == 0)
            {
                return;
            }

            foreach (Prop p in props.Items)
            {
                listBox1.Items.Add(p.Name);
            }

        }

        private void listView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            Prop prop = props[listBox1.SelectedIndex];

            textBox2.Text = prop.Name;
            comboBox1.Text = prop.Type.ToString();
            textBox4.Text = prop.RequestLevel.ToString();
            textBox5.Text = prop.Weight.ToString();
            textBox6.Text = prop.Worth.ToString();

            richTextBox1.Text = prop.Script;
            richTextBox2.Text = prop.Description;
            //prop.ICON = new NImage(textBox17.Text);

            textBox11.Text = prop.Damage.ToString();
            textBox10.Text = prop.Defense.ToString();
            textBox9.Text = prop.Accurate.ToString();
            textBox8.Text = prop.Missrate.ToString();
            textBox7.Text = prop.RangeMin.ToString();
            textBox3.Text = prop.RangeMax.ToString();
            textBox15.Text = prop.Time.ToString();

            textBox12.Text = prop.Load.ToString();
            textBox16.Text = prop.Endure.ToString();

            textBox21.Text = prop.SkinSoundFileName;
            textBox20.Text = prop.BulletSoundFileName;
            textBox19.Text = prop.HitSoundFileName;

            textBox18.Text = prop.SkinFileName;
            textBox14.Text = prop.BulletFileName;
            textBox13.Text = prop.HitFileName;


            textBox23.Text = prop.Square + "";
            textBox22.Text = prop.SquareType + "";

        }

        private void textBox18_Click(object sender, EventArgs e)
        {
            OpenFileDialog fd = new OpenFileDialog();
            fd.RestoreDirectory = true;
            fd.Filter = "*.nani|*.nani";
            if (fd.ShowDialog() == DialogResult.OK)
            {
                textBox18.Text = fd.FileName;
            }
        }

        private void textBox14_Click(object sender, EventArgs e)
        {
            OpenFileDialog fd = new OpenFileDialog();
            fd.RestoreDirectory = true;
            fd.Filter = "*.nani|*.nani";
            if (fd.ShowDialog() == DialogResult.OK)
            {
                textBox14.Text = fd.FileName;
            }
        }

        private void textBox13_Click(object sender, EventArgs e)
        {
            OpenFileDialog fd = new OpenFileDialog();
            fd.RestoreDirectory = true;
            fd.Filter = "*.nani|*.nani";
            if (fd.ShowDialog() == DialogResult.OK)
            {
                textBox13.Text = fd.FileName;
            }
        }

        private void textBox21_TextChanged(object sender, EventArgs e)
        {
            OpenFileDialog fd = new OpenFileDialog();
            fd.RestoreDirectory = true;
            fd.Filter = "*.nsnd|*.nsnd|*.mp3|*.mp3";
            if (fd.ShowDialog() == DialogResult.OK)
            {
                textBox21.Text = fd.FileName;
            }
        }

        private void textBox20_TextChanged(object sender, EventArgs e)
        {
            OpenFileDialog fd = new OpenFileDialog();
            fd.RestoreDirectory = true;
            fd.Filter = "*.nsnd|*.nsnd|*.mp3|*.mp3";
            if (fd.ShowDialog() == DialogResult.OK)
            {
                textBox20.Text = fd.FileName;
            }
        }

        private void textBox19_TextChanged(object sender, EventArgs e)
        {
            OpenFileDialog fd = new OpenFileDialog();
            fd.RestoreDirectory = true;
            fd.Filter = "*.nsnd|*.nsnd|*.mp3|*.mp3";
            if (fd.ShowDialog() == DialogResult.OK)
            {
                textBox19.Text = fd.FileName;
            }
        }
    }
}
